package dto;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import model.YourCart;

public class DtoYourCart {
	
	public static List<YourCart> getAll(String sql , DataManager dataManager) {
		YourCart yourCart = null;
		List<YourCart> yourCarts = new ArrayList<YourCart>() ;
		Connection conn = dataManager.getConnection();
		Statement statement = null;
		ResultSet rs = null;
		if (conn != null) {
			try {
				statement = conn.createStatement();
				
				rs = statement.executeQuery(sql);
				while (rs.next()) {
					yourCart = new YourCart();

					yourCart.setYourCartId(rs.getInt("yourCartId"));
					yourCart.setUserId(rs.getInt("userId"));
					yourCart.setProductId(rs.getInt("productId"));
					yourCart.setDateTimeExport(rs.getString("dateTimeExport"));
					yourCart.setNumber(rs.getInt("number"));
					yourCarts.add(yourCart);
				}
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {

				try {
					if (rs != null) 
						rs.close();
					if(statement != null)
						statement.close();
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}

		return yourCarts;
	}
	
	public static List<YourCart> getByUserId(int userId , DataManager dataManager)
	{
		String sql = "select * from yourcart where userId = "+ userId;
		return getAll(sql, dataManager);
	}
	
	public static boolean insert(YourCart y, DataManager dataManager)
	{
		Connection conn = dataManager.getConnection();
		Statement st = null;
		boolean check = false;
		String sql = "insert into yourcart(userId, productId,dateTimeExport, number) " +
				"values ("+y.getUserId()+", "+y.getProductId()+",'"+y.getDateTimeExport()+"',1)";
		if(conn!= null)
		{
			try {
				st = conn.createStatement();
				
				int rs = st.executeUpdate(sql);
				if(rs > 0) 
					check = true;
			} catch (SQLException e) {
				System.out.println("loi cau sql : " + sql);
			}
			finally
			{
				try {
					
					if(st != null)
						st.close();
					conn.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
		return check;
	}
	
	public static boolean deleteYourCartItem(DataManager dataManager, int yourCartId) {
		Connection conn = null;
		Statement statement = null;
		String sqlQuery = "delete from yourcart where yourCartId="+yourCartId;
		//System.out.print(sqlQuery);
		boolean isOk = true;
		try{
			conn = dataManager.getConnection();
			statement = conn.createStatement();
			statement.executeUpdate(sqlQuery);
		}
		catch (Exception e) {
			System.out.println("loi cau lenh : " + sqlQuery);
			isOk = false;
		}
		finally {
			try {
				dataManager.closeConnection(conn);
				if(statement != null)
					statement.close();
			} catch (SQLException sqle) {
				sqle.printStackTrace();
			}
		}
		return isOk;
	}
	
	public static void deleteAllCartByUserId(DataManager dataManager, int userId)
	{
		List<YourCart> ycs = getByUserId(userId, dataManager);
		for(YourCart c : ycs )
		{
			deleteYourCartItem(dataManager, c.getYourCartId());
		}
	}
	
	public static boolean updateYourCartItem(DataManager dataManager, int yourCartId, int number) {
		Connection conn = null;
		Statement statement = null;
		String sqlQuery = "UPDATE yourcart SET number="+number+" WHERE yourCartId=" + yourCartId;
		
		boolean isOk = true;
		try{
			conn = dataManager.getConnection();
			statement = conn.createStatement();
			statement.executeUpdate(sqlQuery);
		}
		catch (Exception e) {
			System.out.println("loi cau lenh : " + sqlQuery);
			isOk = false;
		}
		finally {
			try {
				dataManager.closeConnection(conn);
				if(statement != null)
					statement.close();
			} catch (SQLException sqle) {
				sqle.printStackTrace();
			}
		}
		return isOk;
	}
	public static boolean checkProduct(int productId , int userId, DataManager dataManager)
	{
		String sql = "select * FROM yourcart where productId = "+productId+" and userId =" + userId;
		return getAll(sql, dataManager).isEmpty()? true:false;
		
	}
	


}
